package com.xu.a2019;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyAdapter adapter;
    private final static String[] weekDays = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    private long time;
    private String TAG = "2019";
    private EditText et1, et2, et3, et4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SqlHelper.init(this, new SqlCallBack());
        time = System.currentTimeMillis();
        ListView lis = findViewById(R.id.gv);
        adapter = new MyAdapter(this, new ArrayList<BlogBean>());
        lis.setAdapter(adapter);
        findViewById(R.id.pre).setOnClickListener(this);
        findViewById(R.id.next).setOnClickListener(this);
        findViewById(R.id.tv_submit).setOnClickListener(this);
        findViewById(R.id.tv_total).setOnClickListener(this);
        et1 = findViewById(R.id.in_content);
        et2 = findViewById(R.id.ho_content);
        et3 = findViewById(R.id.ko_content);
        et4 = findViewById(R.id.z_content);
        initData();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initData() {
        setWeekOfYear(new Date(time));
        Log.d(TAG, "initData: " + time);
        Date date = new Date(time);
        int week = getWeekOfDate(date);
        List<BlogBean> data = new ArrayList<>();
        BlogBean blogBean = new BlogBean();
        blogBean.setWeek("WEEK");
        blogBean.setDate("DATE");
        blogBean.setFlag(2);
        blogBean.setTimesp(0);
        blogBean.setContent("ZERO");
        blogBean.setContent2("KNGT");
        data.add(blogBean);
        for (int i = 0; i < 7; i++) {
            BlogBean tempBean = new BlogBean();
            tempBean.setWeek(weekDays[i]);
            long tempTime = time - (week - i) * 1000 * 60 * 60 * 24;
            Log.d(TAG, "day: " + i + "time" + time);
            SimpleDateFormat format = new SimpleDateFormat("MM-dd", Locale.CHINA);
            String key = format.format(new Date(tempTime));
            tempBean.setDate(key);
            long current = System.currentTimeMillis();
            long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
            if (tempTime < zero) {
                tempBean.setFlag(1);
            } else if (tempTime > zero + 24 * 60 * 60 * 1000) {
                tempBean.setFlag(-1);
            } else {
                tempBean.setFlag(0);
            }
            tempBean.setTimesp((int) (tempTime / 1000));
            List<PhraseCache> caches = SqlHelper.selectByKey(SqlCallBack.TABLE_NAME_PHRASE, key);
            if (caches != null && caches.size() > 0) {
                if (TextUtils.isEmpty(caches.get(0).getmZero())||TextUtils.isEmpty(caches.get(0).getmKnight())){
                    continue;
                }
                tempBean.setContent(caches.get(0).getmZero());
                tempBean.setContent2(caches.get(0).getmKnight());
                tempBean.setFlag(4);
            }
            data.add(tempBean);
        }
        adapter.initData(data);
        adapter.notifyDataSetChanged();
        String date2 = new SimpleDateFormat("yyyy-MM", Locale.CHINA).format(new Date(time));
        List<PhraseCache> list = SqlHelper.selectByKey(SqlCallBack.TABLE_NAME_FAVORITE, date2);
        if (list == null || list.size() == 0) {
            et1.setText("11100");
            et2.setText("-1600");
            et3.setText("-1200");
            et4.setText("-1300");
        } else {
            PhraseCache cache = list.get(0);
            String[] content = cache.getmKnight().split(",");
            et1.setText(content[0]);
            et2.setText(content[1]);
            et3.setText(content[2]);
            et4.setText(cache.getmZero());
        }
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public int getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 2;
        if (w < 0) w = 6;
        Log.d(TAG, "getWeekOfDate: " + w);
        return w;
    }

    private void setWeekOfYear(Date dt) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(7);
        String co = "2019年的第 " + calendar.get(Calendar.WEEK_OF_YEAR) + " 周";
        TextView textView = findViewById(R.id.title_a);
        textView.setText(co);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pre:
                time = time - 7 * 24 * 60 * 60 * 1000;
                initData();
                break;
            case R.id.next:
                time = time + 7 * 24 * 60 * 60 * 1000;
                initData();
                break;
            case R.id.tv_submit:
                List<BlogBean> beans = adapter.getData();
                for (BlogBean bean : beans) {
                    if (bean.getFlag() == 2) continue;
                    if (TextUtils.isEmpty(bean.getContent())||TextUtils.equals("0",bean.getContent())
                            ||TextUtils.isEmpty(bean.getContent2())||TextUtils.equals("0",bean.getContent2())){
                        continue;
                    }
                    PhraseCache cache = new PhraseCache(bean.getContent(),
                            bean.getDate(), bean.getContent2(), bean.getTimesp());
                    List list = SqlHelper.selectByKey(SqlCallBack.TABLE_NAME_PHRASE, bean.getDate());
                    if (list == null || list.size() == 0) {
                        SqlHelper.insert(SqlCallBack.TABLE_NAME_PHRASE, cache);
                    } else {
                        SqlHelper.updateKey(SqlCallBack.TABLE_NAME_PHRASE, cache);
                    }
                }
                String date = new SimpleDateFormat("yyyy-MM", Locale.CHINA).format(new Date(time));
                PhraseCache cache = new PhraseCache(et4.getText().toString(), date,
                        et1.getText().toString() + "," + et2.getText().toString() + "," + et3.getText().toString(),
                        (int) (time / 1000));
                List list = SqlHelper.selectByKey(SqlCallBack.TABLE_NAME_FAVORITE, date);
                if (list == null || list.size() == 0) {
                    SqlHelper.insert(SqlCallBack.TABLE_NAME_FAVORITE, cache);
                } else {
                    SqlHelper.updateKey(SqlCallBack.TABLE_NAME_FAVORITE, cache);
                }
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_total:
                startActivity(new Intent(this, TotalActivity.class));
                break;
            default:
                break;
        }
    }


}
